local joy__pingjian = fk.CreateSkill {

  name = "joy__pingjian",

  tags = {  },

}



joy__pingjian:addEffect("active", {
  name = "joy__pingjian",
  prompt = "#joy__pingjian-active",
  card_num = 0,
  target_num = 0,
  card_filter = Util.FalseFunc,
  can_use = function(self, player)
    return player:usedSkillTimes(joy__pingjian.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local skills = getPingjianSkills(player)
    if #skills == 0 then return false end
    local choices = table.random(skills, 3)
    local skill_name = room:askForChoice(player, choices, joy__pingjian.name, "#joy__pingjian-choice", true)
    local phase_event = room.logic:getCurrentEvent():findParent(GameEvent.Phase)
    if phase_event ~= nil then
      addTYPingjianSkill(player, skill_name)
      phase_event:addCleaner(function()
        removeTYPingjianSkill(player, skill_name)
      end)
    end
  end,
})

joy__pingjian:addEffect(fk.Damaged, {
  name = "#joy__pingjian_trigger",
  events = {fk.Damaged, fk.EventPhaseStart},
  --joy__pingjian,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(joy__pingjian.name) or player ~= target then return false end
    if event == fk.Damaged then
      return true
    elseif event == fk.EventPhaseStart then
      return player.phase == Player.Finish
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, joy__pingjian.name)
    player:broadcastSkillInvoke(joy__pingjian.name)
    local skills = getPingjianSkills(player, event)
    if #skills == 0 then return false end
    local choices = table.random(skills, 3)
    local skill_name = room:askForChoice(player, choices, joy__pingjian.name, "#joy__pingjian-choice", true)
    local skill = Fk.skills[skill_name]
    if skill == nil then return false end
    local _skill = skill
    if not _skill:isInstanceOf(TriggerSkill) then
      _skill = table.find(_skill.related_skills, function (s)
        return s:isInstanceOf(TriggerSkill)
      end)
      if not _skill then return end
    end
    addTYPingjianSkill(player, skill_name)
    if _skill:triggerable(event, target, player, data) then
      _skill:trigger(event, target, player, data)
    end
    removeTYPingjianSkill(player, skill_name)
  end,
})
joy__pingjian:addEffect(fk.EventPhaseStart, {
  name = "#joy__pingjian_trigger",
  events = {fk.Damaged, fk.EventPhaseStart},
  --joy__pingjian,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(joy__pingjian.name) or player ~= target then return false end
    if event == fk.Damaged then
      return true
    elseif event == fk.EventPhaseStart then
      return player.phase == Player.Finish
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, joy__pingjian.name)
    player:broadcastSkillInvoke(joy__pingjian.name)
    local skills = getPingjianSkills(player, event)
    if #skills == 0 then return false end
    local choices = table.random(skills, 3)
    local skill_name = room:askForChoice(player, choices, joy__pingjian.name, "#joy__pingjian-choice", true)
    local skill = Fk.skills[skill_name]
    if skill == nil then return false end
    local _skill = skill
    if not _skill:isInstanceOf(TriggerSkill) then
      _skill = table.find(_skill.related_skills, function (s)
        return s:isInstanceOf(TriggerSkill)
      end)
      if not _skill then return end
    end
    addTYPingjianSkill(player, skill_name)
    if _skill:triggerable(event, target, player, data) then
      _skill:trigger(event, target, player, data)
    end
    removeTYPingjianSkill(player, skill_name)
  end,
})
joy__pingjian:addEffect(fk.DamageInflicted, {
  refresh_events = {fk.DamageInflicted},
  can_refresh = function(self, event, target, player, data)
    return target == player and not player.faceup
  end,
  on_refresh = function(self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.jiushi_check = true
  end,
})

joy__pingjian:addEffect("invalidity", {
  name = "#joy__pingjian_invalidity",
  invalidity_func = function(self, player, skill)
    local pingjian_skill_times = player:getTableMark( "joy__pingjian_skill_times")
    return table.find(pingjian_skill_times, function (pingjian_record)
      if #pingjian_record == 2 then
        local skill_name = pingjian_record[1]
        if skill.name == skill_name or not table.every(skill.related_skills, function (s)
          return s.name ~= skill_name end) then
            return player:usedSkillTimes(skill_name) > pingjian_record[2]
        end
      end
    end)
  end
})

return joy__pingjian